<?php
/**
 * This file contains {@link Domain51_App_Css}
 *
 * @package     Domain51
 * @subpackage  Domain51_Css
 * @license     Released under the same license as Solar.
 */


/**
 * Simple CSS controller for pulling a CSS file out of the Solar file system.
 *
 * This is to simplify the process of packaging CSS files with an application or
 * library.
 *
 * @author      Travis Swicegood <development [at] domain51 [dot] com>
 * @package     Domain51
 * @subpackage  Domain51_Css
 * @license     Released under the same license as Solar.
 */
class Domain51_App_Css extends Solar_Base
{
    /**
     * Handle instantiation
     *
     * @param   array|null
     * @return  void
     */
    public function __construct($config = null) {
        parent::__construct($config);
    }
    
    /**
     * Return CSS check
     *
     * Performs basic check to insure it knows about the requested file and
     * that it exists.  If not, it returns a 404 error and a basic 404 not found
     * error message.
     *
     * @param   Solar_Uri_Action
     * @return  string
     *
     * @todo Make error messages use locale code
     */
    public function fetch($spec = null) {
        if (is_null($spec)) {
            return;
        }
        
        $cssName = $spec->path[0];
        
        // Check to see if this $cssName is configured
        if (!isset($this->_config[$cssName])) {
            $cleanCss = htmlentities($cssName);
            $errorMsg = "404 - CSS file {$cleanCss} unknown";
            header("HTTP/1.1 {$errorMsg}", true, 404);
            return "<h1>$errorMsg</h1>";
        }
        
        // Check to see if this $cssName's value is a real file
        if (!file_exists($this->_config[$cssName])) {
            $cleanCss = htmlentities($cssName);
            $errorMsg = "404 - CSS file {$cleanCss} not found";
            header("HTTP/1.1 {$errorMsg}", true, 404);
            return "<h1>$errorMsg</h1>";
        }
        
        // We're good, set content-type and return.
        header('Content-Type: text/css; charset=UTF-8', true);
        return file_get_contents($this->_config[$cssName]);
    }
}

?>
